// Mongoose
const mongoose = require('mongoose')

// Schema
const MemorySchema = require('../schema/MemorySchema');
const ErrorSchema = require('../schema/ErrorSchema');
const StaticSchema = require('../schema/StaticSchema');
const XHRSchema = require('../schema/XHRSchema');

//Utils
const utils = require('./index.js');

module.exports = {
    /**
     * 上报
     * @param {Object} params 
     */
    report(params) {
        return new Promise((resolve, reject) => {
            switch (params.type) {
                case 'static':
                    const static = new StaticSchema(
                        Object.assign(params.info, {
                            _id: utils.guid(),
                            createTime: utils.timestampToTime(new Date().getTime())
                        })
                    );
                    static.save()
                        .then(res => {
                            resolve(res);
                        })
                        .catch(e => {
                            reject(e);
                        })
                    break;
                case 'error':
                    const err = new ErrorSchema(
                        Object.assign(params.info, {
                            _id: utils.guid(),
                            createTime: utils.timestampToTime(new Date().getTime())
                        })
                    );
                    err.save()
                        .then(res => {
                            resolve(res);
                        })
                        .catch(e => {
                            reject(e);
                        })
                    break;
                case 'memory':
                    const memory = new MemorySchema(
                        Object.assign(params.info, {
                            _id: utils.guid(),
                            createTime: utils.timestampToTime(new Date().getTime())
                        })
                    );
                    memory.save()
                        .then(res => {
                            resolve(res);
                        })
                        .catch(e => {
                            reject(e);
                        })
                    break;
                case 'xhr':
                    const xhr = new XHRSchema(
                        Object.assign(params.info, {
                            _id: utils.guid(),
                            createTime: utils.timestampToTime(new Date().getTime())
                        })
                    );
                    xhr.save()
                        .then(res => {
                            resolve(res);
                        })
                        .catch(e => {
                            reject(e);
                        })
                    break;
            }
        })
    },
    /**
     * 获取数据库数据信息
     * @param {*} days 日期数组 
     * @param {*} type 类型 
     * @paramTemplate ["2019-08-06","2019-08-07","2019-08-08","2019-08-09","2019-08-10"]
     */
    getDataByDays(days, type) {
        let realResult = [];
        let result = Promise.resolve();
        days.forEach((item, i) => {
            let qs = new RegExp(item);
            result = result.then(() => {
                switch (type) {
                    case 'memory':
                        return MemorySchema.where('createTime', qs)
                            .then(res => {
                                realResult.push({
                                    day: item,
                                    data: res
                                });
                            })
                        break;
                    case 'static':
                        return StaticSchema.where('createTime', qs)
                            .then(res => {
                                realResult.push({
                                    day: item,
                                    data: res
                                });
                            })
                        break;
                    case 'error':
                        return ErrorSchema.where('createTime', qs)
                            .then(res => {
                                realResult.push({
                                    day: item,
                                    data: res
                                });
                            })
                        break;
                    case 'xhr':
                        return XHRSchema.where('createTime', qs)
                            .then(res => {
                                realResult.push({
                                    day: item,
                                    data: res
                                });
                            })
                        break;
                }
            })
        })
        return result.then(() => {
            return realResult
        })
    }
}